package io.renren.wechat.service.impl;

import com.alibaba.druid.util.StringUtils;
import io.renren.wechat.entity.ChatMsgEntity;
import io.renren.wechat.entity.UsersEntity;
import io.renren.wechat.service.UsersService;
import io.renren.wechat.vo.FriendVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.wechat.dao.MyFriendsDao;
import io.renren.wechat.entity.MyFriendsEntity;
import io.renren.wechat.service.MyFriendsService;


@Service("myFriendsService")
public class MyFriendsServiceImpl extends ServiceImpl<MyFriendsDao, MyFriendsEntity> implements MyFriendsService {

    @Autowired
    UsersService usersService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<MyFriendsEntity> page = this.page(
//                new Query<MyFriendsEntity>().getPage(params),
//                new QueryWrapper<MyFriendsEntity>()
//        );
//
//        return new PageUtils(page);
//    }
        QueryWrapper<MyFriendsEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key)
                    .or().eq("my_user_id",key)
                    .or().eq("my_friend_user_id",key)
            ;
        }
        IPage<MyFriendsEntity> page = this.page(
                new Query<MyFriendsEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<FriendVo> seekFriend(String myUserId) {

        List<MyFriendsEntity> myFriendsEntities = baseMapper.selectList(new QueryWrapper<MyFriendsEntity>().eq("my_user_id", myUserId));

        List<FriendVo> list = myFriendsEntities.stream().map(v -> {
            FriendVo friendVo = new FriendVo();
            //查出我所有的好友
            UsersEntity one = usersService.getOne(new QueryWrapper<UsersEntity>().eq("id", v.getMyFriendUserId()));
            friendVo.setId(one.getId());
            friendVo.setUsername(one.getUsername());
            friendVo.setNickname(one.getNickname());
            return friendVo;
        }).collect(Collectors.toList());


//            FriendVo friendVo = new FriendVo();
//            List<UsersEntity> usersEntities = usersService.list(new QueryWrapper<UsersEntity>().eq("id", v.getId()));
//            BeanUtils.copyProperties(usersEntities,friendVo);
//            return friendVo;
//        });

        return list;
    }

    @Override
    public void remove(String id) {
        baseMapper.delete(new QueryWrapper<MyFriendsEntity>().eq("my_friend_user_id",id));
    }
}